using System;
using System.Collections;
using System.Data;
using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarOccupationDB
    {
        public CarOccupationDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarOccupation Occupation)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarOccupationInsertUpdateSingleItem]";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
            _sPara.Add("@ProviderCarOccupationId", Occupation.ProviderOccupationId);
            _sPara.Add("@ProviderCompanyId", Occupation.ProviderCompanyId);
            _sPara.Add("@CXOccupationId", Occupation.CXOccupationId);
            _sPara.Add("@Description", Occupation.Description);
            _sPara.Add("@Code", Occupation.Code);
            _sPara.Add("@OccupationRate", Occupation.OccupationRate);
            _sPara.Add("@RefCEUserId", Occupation.RefCEUserId);
            _sPara.Add("@Status", Occupation.Status);
            _sPara.Add("@Finfo1", Occupation.FInfo1);
            _sPara.Add("@Dinfo1", Occupation.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete(Int64 ProviderCarOccupationId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarOccupationDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarOccupationId", ProviderCarOccupationId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }

        public bool CheckProviderCarOccupationDuplicate(Int64 CurrentProviderOccupationId, Int64 ProviderCompanyId, int CXOccupationId)
        {
            bool _retValue = false;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarOccupationDuplicateSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarOccupationId", CurrentProviderOccupationId);
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXOccupationId", CXOccupationId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _retValue = true;
                }
            }
            return _retValue;
        }

        public CarOccupationList GetProviderOccupationList(Int64 ProviderCompanyId)
        {
            CarOccupationList _Occupationlist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarOccupationSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _Occupationlist = new CarOccupationList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarOccupation _Occupation = new CarOccupation();
                        _Occupation = FillDataRecord(_dt, i);
                        _Occupationlist.Add(_Occupation);
                    }
                }
            }
            return _Occupationlist;
        }


        public CarOccupation GetProviderOccupation(Int64 ProviderCompanyId, Int32 CXOccupationId)
        {
            CarOccupation _Occupation = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarOccupationSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXOccupationId", CXOccupationId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _Occupation = new CarOccupation();
                    _Occupation = FillDataRecord(_dt, 0);
                }
            }
            return _Occupation;
        }

        public CarOccupation GetProviderOccupation(Int64 ProviderCarOccupationId)
        {
            CarOccupation _Occupation = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarOccupationSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarOccupationId", ProviderCarOccupationId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _Occupation = new CarOccupation();
                    _Occupation = FillDataRecord(_dt, 0);
                }
            }
            return _Occupation;
        }

        public CarOccupation GetProviderDeclinedOccupation(Int64 ProviderCompanyId, Int32 CXOccupationId)
        {
            CarOccupation _Occupation = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarOccupationSelectSingleItemDeclined";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXOccupationId", CXOccupationId);


            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _Occupation = new CarOccupation();
                    _Occupation = FillDataRecord(_dt, 0);
                }
            }
            return _Occupation;
        }

        private CarOccupation FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarOccupation myOccupation = new CarOccupation();
            myOccupation.ProviderOccupationId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarOccupationId"].ToString());
            myOccupation.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myOccupation.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myOccupation.CXOccupationId = Convert.ToInt32(myDataTable.Rows[RowId]["CXOccupationId"].ToString());
            myOccupation.OccupationRate = Convert.ToSingle(myDataTable.Rows[RowId]["OccupationRate"].ToString());
            myOccupation.Code = Convert.ToInt32(myDataTable.Rows[RowId]["Code"].ToString());
            myOccupation.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myOccupation.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myOccupation.CreatedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["CreatedDate"].ToString());
            myOccupation.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myOccupation.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myOccupation.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myOccupation.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myOccupation;
        }
        
    }
}